cmake_minimum_required(VERSION 3.15)
project(ly LANGUAGES CXX)

set(target_name ly)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ==================== 依赖查找 ====================
find_package(Qt5 COMPONENTS Core Widgets Network Sql Concurrent Xml REQUIRED CONFIG)
find_package(Clipper2 REQUIRED)
find_package(RapidJSON REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(freetype REQUIRED)
find_package(glm REQUIRED)
find_package(spdlog REQUIRED)
find_package(EnTT REQUIRED)
find_package(vsg REQUIRED)
find_package(gdstk REQUIRED)
find_package(libspatialindex REQUIRED)
find_package(GTest REQUIRED)

# ==================== 源文件 ====================
file(GLOB_RECURSE SRC_FILES ly/*.cpp)
# ⭐ 重要：不要包含 .hpp 到库源文件！
# file(GLOB_RECURSE HEADER_FILES ly/*.hpp)  # ← 删除这行

add_library(${target_name} SHARED ${SRC_FILES})
# add_library(${target_name} ${SRC_FILES})

# ==================== 关键修复：导出宏定义 ⭐ ====================
# ✅ 正确方式：定义 BUILDING_LY 宏（对应 export.hpp 中的条件）
target_compile_definitions(${target_name}
    PRIVATE BUILDING_LY  # ← 让 export.hpp 知道正在构建 ly 库
    # ⭐ 可选：为使用者提供静态/动态提示
    INTERFACE "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:LY_STATIC_DEFINE>"
)

# ==================== 头文件路径 ====================
target_include_directories(${target_name} PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>  # ← 注意：不是 /ly，确保 #include "ly/export.hpp" 能找到
    $<INSTALL_INTERFACE:include>

    ${vsg_INCLUDE_DIR}
    ${gdstk_INCLUDE_DIR}
    ${GTest_INCLUDE_DIR}
    ${libspatialindex_INCLUDE_DIR}
)

# ==================== 链接依赖 ====================
target_link_libraries(${target_name} PRIVATE
    Qt5::Core Qt5::Widgets Qt5::Network Qt5::Sql Qt5::Concurrent Qt5::Xml
    Clipper2::clipper2 Clipper2::clipper2z
    rapidjson
    nlohmann_json::nlohmann_json
    Freetype::Freetype
    glm::glm
    spdlog::spdlog
    EnTT::EnTT
    vsg::vsg
    gdstk::gdstk
    libspatialindex::spatialindex
)

# ==================== 导出控制（Linux/macOS）⭐ ====================
if(NOT WIN32)
    set_target_properties(${target_name} PROPERTIES
        CXX_VISIBILITY_PRESET hidden    # 默认隐藏符号
        VISIBILITY_INLINES_HIDDEN ON    # 隐藏内联函数
    )
endif()

# ==================== 安装配置 ====================
install(TARGETS ${target_name}
    EXPORT lyTargets
    RUNTIME DESTINATION bin          # .dll / .so
    LIBRARY DESTINATION lib          # .so (Linux)
    ARCHIVE DESTINATION lib          # .lib (Windows import lib)
    PUBLIC_HEADER DESTINATION include
)

# ⭐ 改为安装头文件目录（推荐扁平结构）
install(DIRECTORY ly/ DESTINATION include/ly
    FILES_MATCHING PATTERN "*.hpp"
)

# 导出 Config 文件
install(EXPORT lyTargets
    FILE lyTargets.cmake
    DESTINATION lib/cmake/ly
)

include(CMakePackageConfigHelpers)
configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/lyConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/lyConfig.cmake
    INSTALL_DESTINATION lib/cmake/ly
)

install(FILES 
    ${CMAKE_CURRENT_BINARY_DIR}/lyConfig.cmake
    DESTINATION lib/cmake/ly
)

# ==================== 使用示例：测试程序（验证 dllimport）⭐ ====================
add_subdirectory(unit-tests)
